/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dnbW.uiW.queryW.visual;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.List;
import java.util.ArrayList;
import dnbW.dbW.PathExpression;


/**
 * This object encapsulates a processor to process and store the merging process of an incoming
 * path and other existing paths
 * @author Truong Ba Quan
 */
public class MergeProcessor {
    
    public static List<PathExpression> previousPath = new ArrayList<PathExpression>();
    
    public static boolean processMergePossible() {
        
        boolean isMergePossible = false;

        if (previousPath.size() != 0) {
            ArrayList<PathExpression> pathExpressions = new ArrayList<PathExpression>();
            pathExpressions.add(controller.Controller.currentPath);
            
            pathExpressions.add(previousPath.get(0));
            
            PathExpression[] pathArray = new PathExpression[previousPath.size() + 1];
            int lca = PathExpression.lcp(pathExpressions.toArray(pathArray));

            int lcaWithOutput;
            if (controller.Controller.initialPath.isSameDataSource(controller.Controller.currentPath)) {
                pathExpressions.add(controller.Controller.initialPath);
                pathArray = new PathExpression[previousPath.size() + 2];
                lcaWithOutput = PathExpression.lcp(pathExpressions.toArray(pathArray));
            }
            else lcaWithOutput = -1;
            if (lca > lcaWithOutput) {
                isMergePossible = true;
            }
        }
        return isMergePossible;
    }
}
